package 刷题.leedcode150;

import java.util.Stack;

public class Solution2 {
    public int evalRPN(String [] tokens){
        if(tokens.length == 0){
            return 0;
        }
        Stack<Integer> stack = new Stack<Integer>();
        int n = tokens.length;
        for (int i = 0; i < n; i++) {
            if(("+".equals(tokens[i])) ||
                    ("-".equals(tokens[i])) ||
                    ("*".equals(tokens[i])) ||
                    ("/".equals(tokens[i])) ){
                int num1 = stack.pop();
                int num2 = stack.pop();
                if("+".equals(tokens[i])){
                    stack.push(num2 + num1);
                }
                if(("-".equals(tokens[i]))){
                    stack.push(num2 - num1);
                }
                if(("*".equals(tokens[i]))){
                    stack.push(num2 * num1);
                }
                if(("/".equals(tokens[i]))){
                    stack.push(num2 / num1);
                }
            }else{//数字
                //用Integer类型数据表示一个数字
                stack.push(Integer.valueOf(tokens[i]));
            }
        }
        return stack.pop();
    }
}
